/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agentsystem;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Florian Fink
 */
public class Product {
    private ArrayList<Integer> properties;
    static int numberOfProductProperties;
    static Random rand = new Random();
    private Product(){
        properties=new ArrayList<Integer>(numberOfProductProperties);
    }
    
    public static void setNumberOfProductProperties(int no){
        numberOfProductProperties = no;
        rand.setSeed(1237639214);
    }
    
    public static Product createRandom(){
        //create initial product randomly
        Product newProduct=new Product();
        for (int i = 0; i < numberOfProductProperties; i++) {
            newProduct.properties.add(rand.nextInt(100));
        }
        return newProduct;
    }
    public Product(String productString){
        properties=new ArrayList<Integer>(numberOfProductProperties);
        int currentIndex=1;
        while(productString.charAt(currentIndex)!=')'){
            int endIndex=productString.indexOf(';', currentIndex);
            String subString=productString.substring(currentIndex, endIndex);
            properties.add(Integer.parseInt(subString));
            currentIndex=endIndex+1;
        }
    }
    
    public int getProperty(int i){
        return this.properties.get(i);
    }
    
    
    @Override
    public String toString(){
        String ret="(";
        
        for(int i=0;i<numberOfProductProperties;i++)
            ret=ret+properties.get(i)+";";
        
        return ret+")";
    }
    
    public void moveTowardsOtherProduct(Product goalProduct){
        for(int i=0;i<numberOfProductProperties;i++){
            if(properties.get(i) != goalProduct.properties.get(i)){
                double moveTo = (double)(properties.get(i)*4+goalProduct.properties.get(i))/5.0;
                if((int)moveTo == properties.get(i)){
                    if(moveTo < (double) properties.get(i)){
                        moveTo -= 1.0;
                    }else if(moveTo > (double) properties.get(i)){
                        moveTo += 1.0;
                    }else{
                        System.out.println("WTFWTWFWWTFWTWFWWTFWTWFWWTFWTWFWWTFWTWFWWTFWTWFWWTFWTWFWWTFWTWFW");
                        System.out.println(moveTo);
                        System.out.println(properties.get(i));
                    }
                }
                properties.set(i, (int)moveTo);
            }
            //TODO: maybe make movement factor more dynamic
        }
    }
    public double differenceTo(Product product){
        int sum=0;
        for(int i=0;i<numberOfProductProperties;i++){
            int differenceToProperty=product.properties.get(i)-properties.get(i);
            differenceToProperty*=differenceToProperty; // ~euclidean distance
            sum+=differenceToProperty;
        }
        return Math.sqrt(sum);
    }
}
